<?php

class Application_Model_Emailconfirm
{


}

class Db_Emailconfirm extends Zend_Db_Table 
{ 

    protected $_name = 'emailconfirm';

    protected function MakeHash($email)
    {
        return md5($email.date('c').rand());
    }

    public function getIdByHash($hash)
    {
	$data=$this->fetchRow('checkhash="'.$hash.'"');
	return $data['id'];
    }	

    public function isEmailConfirmed($id)
    {
	return ( $this->fetchAll('id="'.$id.'"')->count() == 0 ) ? true : false;
    }


    public function addEmailConfirm($id,$email)
    {
	if ($this->IsEmailConfirmed($id))
	{
	    $hash=$this->MakeHash($email);
	    $setdata = array
	    (
		'id' 	=> $id,
		'email' 	=> $email,
		'checkhash' => $hash,
		'startdate' => new Zend_Db_Expr('NOW()')
	    );
	    $this->insert($setdata);
	    return $hash;
	}
	else
	    return false;
    }

    public function delEmailConfirmByHash($hash)
    {
	$id=$this->getIdByHash($hash);
	return ( $this->delete('checkhash="'.$hash.'"') > 0 ) ? $id : false ;
    }

    public function delEmailConfirmById($id)
    {
	return ( $this->delete('id="'.$id.'"') > 0 ) ? true : false ;
    }

}
